package tp.pr5.gui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.GridBagLayout;
import java.awt.event.ContainerAdapter;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

import tp.pr5.GameController;
import tp.pr5.GameObserver;
import tp.pr5.MensajesConst;
import tp.pr5.RoomInfo;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Dimension;

public class MainWindow extends JFrame implements GameObserver
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6937031334160866618L;
	private GameController _gameController;
	private PlayerPanel _playerPanel;
	private MapPanel _mapPanel;
	private InfoPanel infoPanel;

	public MainWindow(GameController game)
	{
		setResizable(true);
		setMinimumSize(new Dimension(1000, 650));
		this._gameController = game;
		getContentPane().addContainerListener(new ContainerAdapter()
		{

		});
		// --- PARA EVITAR UN NULLPOINTER
		_playerPanel = new PlayerPanel(this._gameController);
		_mapPanel = new MapPanel();
		// ---

		setTitle("Aventura Grafica");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]
		{ 220, 514, 0 };
		gridBagLayout.rowHeights = new int[]
		{ 142, 309, 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[]
		{ 0.0, 1.0, Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[]
		{ 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE };
		getContentPane().setLayout(gridBagLayout);

		JPanel actions = new ActionPanel(this._gameController, this._playerPanel, this._mapPanel);
		GridBagConstraints gbc_actions = new GridBagConstraints();
		gbc_actions.insets = new Insets(0, 0, 5, 5);
		gbc_actions.fill = GridBagConstraints.BOTH;
		gbc_actions.gridx = 0;
		gbc_actions.gridy = 0;
		getContentPane().add(actions, gbc_actions);

		// DEBERIA IR AQUI _player
		GridBagConstraints gbc_player = new GridBagConstraints();
		gbc_player.fill = GridBagConstraints.BOTH;
		gbc_player.insets = new Insets(0, 0, 5, 0);
		gbc_player.gridx = 1;
		gbc_player.gridy = 0;
		getContentPane().add(_playerPanel, gbc_player);

		GridBagConstraints gbc_mapa = new GridBagConstraints();
		gbc_mapa.insets = new Insets(0, 0, 5, 0);
		gbc_mapa.gridwidth = 2;
		gbc_mapa.fill = GridBagConstraints.BOTH;
		gbc_mapa.gridx = 0;
		gbc_mapa.gridy = 1;
		getContentPane().add(_mapPanel, gbc_mapa);

		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");

		menuBar.add(mnFile);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addMouseListener(new MouseAdapter()
		{
			@Override public void mouseClicked(MouseEvent e)
			{
				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			}

		});
		mnFile.add(mntmExit);
		infoPanel = new InfoPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 2;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 4;
		getContentPane().add(infoPanel, gbc_panel);
		//Anyadimos los observadores
		
		this._gameController.registerGameObserver(this);
		this._gameController.registerMapObserver(this._mapPanel);
		this._gameController.registerPlayerObserver(this._playerPanel);
		this._gameController.registerGameObserver(infoPanel);
		this._gameController.registerMapObserver(infoPanel);
		this._gameController.registerPlayerObserver(infoPanel);
		
		
		
		
		
		
	}

	@Override public void gameError(String msg)
	{
		JOptionPane.showMessageDialog(null, msg);

	}

	@Override public void gameHelp()
	{
		JOptionPane.showMessageDialog(null, MensajesConst.help);

	}

	@Override public void gameOver(boolean win)
	{
		JOptionPane.showMessageDialog(null, MensajesConst.finJuego);

	}

	@Override public void gameQuit()
	{
		JOptionPane.showMessageDialog(null, MensajesConst.adios);

	}

	@Override public void gameStart(RoomInfo initialRoom, int playerPoints,
			int playerHealth)
	{
		this._playerPanel.playerUpdate(playerHealth, playerPoints);
		this._mapPanel.roomEntered(null, initialRoom);
	}
}